<!DOCTYPE html>




<html class="theme-next gemini" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.3" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.3">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.3">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.3" color="#222">





  <meta name="keywords" content="AutoValue," />





  <link rel="alternate" href="/atom.xml" title="流水不腐小夏" type="application/atom+xml" />






<meta name="description" content="Android Model正确使用姿势——AutoValue [TOC] 最近看到几篇博客是关于AutoValue的，然后自己十分喜欢，一下子觉的这样写代码很优雅，所以决定自己也写一篇文章介绍下AutoValue。 本文最先发表于Github，如有转载，请注明转载出处。 前言首先说Android Model，在开发中网络请求，以及数据库操作等，我们都会定义一个Model，不同人对这个的说法不一样，">
<meta name="keywords" content="AutoValue">
<meta property="og:type" content="article">
<meta property="og:title" content="Android Model正确使用姿势——AutoValue">
<meta property="og:url" content="http://mycommons.cn/2017/05/11/AutoValue/index.html">
<meta property="og:site_name" content="流水不腐小夏">
<meta property="og:description" content="Android Model正确使用姿势——AutoValue [TOC] 最近看到几篇博客是关于AutoValue的，然后自己十分喜欢，一下子觉的这样写代码很优雅，所以决定自己也写一篇文章介绍下AutoValue。 本文最先发表于Github，如有转载，请注明转载出处。 前言首先说Android Model，在开发中网络请求，以及数据库操作等，我们都会定义一个Model，不同人对这个的说法不一样，">
<meta property="og:locale" content="zh-Hans">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/1.png">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/2.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/3.png">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/4.gif">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/5.png">
<meta property="og:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/6.png">
<meta property="og:updated_time" content="2017-10-25T11:45:33.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Android Model正确使用姿势——AutoValue">
<meta name="twitter:description" content="Android Model正确使用姿势——AutoValue [TOC] 最近看到几篇博客是关于AutoValue的，然后自己十分喜欢，一下子觉的这样写代码很优雅，所以决定自己也写一篇文章介绍下AutoValue。 本文最先发表于Github，如有转载，请注明转载出处。 前言首先说Android Model，在开发中网络请求，以及数据库操作等，我们都会定义一个Model，不同人对这个的说法不一样，">
<meta name="twitter:image" content="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/1.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Gemini',
    version: '5.1.3',
    sidebar: {"position":"left","display":"always","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":false,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: 'GRLHBBCD9E',
      apiKey: '759ab3f5c09b132b3ed4842a44c53c45',
      indexName: 'mycommons',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://mycommons.cn/2017/05/11/AutoValue/"/>





  <title>Android Model正确使用姿势——AutoValue | 流水不腐小夏</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?55458ffca1f60dd283f11b9a7f82b262";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">流水不腐小夏</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">http://mycommons.cn</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404.html" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  
  <div class="algolia-popup popup search-popup">
    <div class="algolia-search">
      <div class="algolia-search-input-icon">
        <i class="fa fa-search"></i>
      </div>
      <div class="algolia-search-input" id="algolia-search-input"></div>
    </div>

    <div class="algolia-results">
      <div id="algolia-stats"></div>
      <div id="algolia-hits"></div>
      <div id="algolia-pagination" class="algolia-pagination"></div>
    </div>

    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
  </div>




    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://mycommons.cn/2017/05/11/AutoValue/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="流水不腐小夏">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.jpeg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="流水不腐小夏">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">Android Model正确使用姿势——AutoValue</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-11T19:38:51+08:00">
                2017-05-11
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/Libs/" itemprop="url" rel="index">
                    <span itemprop="name">Libs</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2017/05/11/AutoValue/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count disqus-comment-count"
                        data-disqus-identifier="2017/05/11/AutoValue/" itemprop="commentCount"></span>
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv">本文总阅读量
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次
            </span>
          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h1 id="Android-Model正确使用姿势——AutoValue"><a href="#Android-Model正确使用姿势——AutoValue" class="headerlink" title="Android Model正确使用姿势——AutoValue"></a>Android Model正确使用姿势——AutoValue</h1><hr>
<p>[TOC]</p>
<p>最近看到几篇博客是关于AutoValue的，然后自己十分喜欢，一下子觉的这样写代码很<strong>优雅</strong>，所以决定自己也写一篇文章介绍下AutoValue。</p>
<p><strong>本文最先发表于<a href="https://github.com/LiushuiXiaoxia/AutoValueDemo" target="_blank" rel="external">Github</a>，如有转载，请注明转载出处。</strong></p>
<h1 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h1><p>首先说Android Model，在开发中网络请求，以及数据库操作等，我们都会定义一个Model，不同人对这个的说法不一样，比如有Entry，Bean，Pojo。</p>
<p>然后开发的过程中会遇到下面问题：</p>
<ul>
<li><p>构成方法：自定义构造方法，如果实体比较复杂，可能会用到工厂模式或者是建造者模式</p>
</li>
<li><p>序列化：比如实现Serializable接口，Parcelable接口。</p>
</li>
<li><p>Json解析：有时候直接使用的是json数据，比如@SerializedName注解。</p>
</li>
<li><p>自定义方法：对Model的字段有setter，getter方法，toString的实现，在处理hash的时候，需要实现equals和hashcode方法。</p>
</li>
</ul>
<p>以上这么问题，其实在Eclipse和Android Studio中都是有快捷功能帮我们自动生成，后面的代码示例，就是我用Android Studio自动生成的。</p>
<p>比如下面一个User类是我们的本次示例的一个Model，如果按照正常的写法，是这样的。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">implements</span> <span class="title">Serializable</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@SerializedName</span>(<span class="string">"id"</span>)</div><div class="line">    <span class="keyword">private</span> <span class="keyword">int</span> id;</div><div class="line"></div><div class="line">    <span class="meta">@SerializedName</span>(<span class="string">"name"</span>)</div><div class="line">    <span class="keyword">private</span> String name;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> id;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setId</span><span class="params">(<span class="keyword">int</span> id)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.id = id;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> name;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">setName</span><span class="params">(String name)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.name = name;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object o)</span> </span>&#123;</div><div class="line">        <span class="keyword">if</span> (<span class="keyword">this</span> == o) <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">        <span class="keyword">if</span> (o == <span class="keyword">null</span> || getClass() != o.getClass()) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line"></div><div class="line">        User user = (User) o;</div><div class="line"></div><div class="line">        <span class="keyword">if</span> (id != user.id) <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">        <span class="keyword">return</span> name != <span class="keyword">null</span> ? name.equals(user.name) : user.name == <span class="keyword">null</span>;</div><div class="line"></div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">int</span> result = id;</div><div class="line">        result = <span class="number">31</span> * result + (name != <span class="keyword">null</span> ? name.hashCode() : <span class="number">0</span>);</div><div class="line">        <span class="keyword">return</span> result;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="string">"User&#123;"</span> +</div><div class="line">                <span class="string">"id="</span> + id +</div><div class="line">                <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</div><div class="line">                <span class="string">'&#125;'</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="简介"><a href="#简介" class="headerlink" title="简介"></a>简介</h1><p>官方文档给出的解释是这样的，大致意思是说是一个生成Java不可变的值类型工具，仔细研读源代码后，使用的技术是Java Apt，这个后面再做详细解释。</p>
<blockquote>
<p>AutoValue - Immutable value-type code generation for Java 1.6+.</p>
</blockquote>
<h1 id="简单使用"><a href="#简单使用" class="headerlink" title="简单使用"></a>简单使用</h1><p>按照上面的例子，如果是AutoValue，代码是这样的。</p>
<p>首先需要在Android项目里面引入apt功能，在项目根目录的gradle中添加，</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div></pre></td><td class="code"><pre><div class="line">buildscript &#123;</div><div class="line">    repositories &#123;</div><div class="line">        jcenter()</div><div class="line">    &#125;</div><div class="line">    dependencies &#123;</div><div class="line">        classpath &apos;com.android.tools.build:gradle:2.2.2&apos;</div><div class="line">        // 引入apt插件</div><div class="line">        classpath &apos;com.neenbedankt.gradle.plugins:android-apt:1.8&apos;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>其次在module(一般是app目录)中gradle使用apt插件。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">apply plugin: &apos;com.android.application&apos;</div><div class="line">apply plugin: &apos;com.neenbedankt.android-apt&apos;</div></pre></td></tr></table></figure>
<p>最后加入AutoValue依赖。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">dependencies &#123;</div><div class="line">    provided &apos;com.google.auto.value:auto-value:1.3&apos;</div><div class="line">    apt &apos;com.google.auto.value:auto-value:1.3&apos;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>修改User类，如下所示，User已经变成了一个抽象类，类似于使用Retrofit一样，申明已经变成了一个接口，然后实现类是由AutoValue生成的代码。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> com.google.auto.value.AutoValue;</div><div class="line"></div><div class="line"><span class="meta">@AutoValue</span></div><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">int</span> <span class="title">id</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> String <span class="title">name</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> User <span class="title">newInstance</span><span class="params">(<span class="keyword">int</span> id, String name)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_User(id, name);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>我们可以看看AutoValue到底干了什么？</p>
<p>AutoValue会自动生成一个AutoValue_User，这个类是继承了上面申明的User类，这个是默认default的访问权限，那么在其他package中是无法访问的，这样在其他代码里面也不会看到这么奇怪的名字。</p>
<p>同时所有的字段都是final类型，如果字段是对象类型的，那么还不能为空，这个问题先保留，后面再做详解。因为申明的是final类型，那么所有的字段都是没有setter方法的。</p>
<p>代码里同时也实现了equals、hashcode、toString方法。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div></pre></td><td class="code"><pre><div class="line"> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">AutoValue_User</span> <span class="keyword">extends</span> <span class="title">User</span> </span>&#123;</div><div class="line"></div><div class="line">  <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> id;</div><div class="line">  <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line"></div><div class="line">  AutoValue_User(</div><div class="line">      <span class="keyword">int</span> id,</div><div class="line">      String name) &#123;</div><div class="line">    <span class="keyword">this</span>.id = id;</div><div class="line">    <span class="keyword">if</span> (name == <span class="keyword">null</span>) &#123;</div><div class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> NullPointerException(<span class="string">"Null name"</span>);</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">this</span>.name = name;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="meta">@Override</span></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">id</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> id;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="meta">@Override</span></div><div class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">name</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> name;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="meta">@Override</span></div><div class="line">  <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> <span class="string">"User&#123;"</span></div><div class="line">        + <span class="string">"id="</span> + id + <span class="string">", "</span></div><div class="line">        + <span class="string">"name="</span> + name</div><div class="line">        + <span class="string">"&#125;"</span>;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="meta">@Override</span></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">boolean</span> <span class="title">equals</span><span class="params">(Object o)</span> </span>&#123;</div><div class="line">    <span class="keyword">if</span> (o == <span class="keyword">this</span>) &#123;</div><div class="line">      <span class="keyword">return</span> <span class="keyword">true</span>;</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">if</span> (o <span class="keyword">instanceof</span> User) &#123;</div><div class="line">      User that = (User) o;</div><div class="line">      <span class="keyword">return</span> (<span class="keyword">this</span>.id == that.id())</div><div class="line">           &amp;&amp; (<span class="keyword">this</span>.name.equals(that.name()));</div><div class="line">    &#125;</div><div class="line">    <span class="keyword">return</span> <span class="keyword">false</span>;</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="meta">@Override</span></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">hashCode</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">int</span> h = <span class="number">1</span>;</div><div class="line">    h *= <span class="number">1000003</span>;</div><div class="line">    h ^= <span class="keyword">this</span>.id;</div><div class="line">    h *= <span class="number">1000003</span>;</div><div class="line">    h ^= <span class="keyword">this</span>.name.hashCode();</div><div class="line">    <span class="keyword">return</span> h;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h1 id="Immutable-Value-types"><a href="#Immutable-Value-types" class="headerlink" title="Immutable/Value types"></a>Immutable/Value types</h1><p>刚刚上面说到，所有的字段都是final类型，那么而且实现类也是final的，有个专业术语叫Immutable。</p>
<p>Immutable/Value types 这个概念对有些朋友来说可能还比较陌生，简单来说就是一个数据对象一旦构造完成，就再也无法修改了。</p>
<p>这样有什么好处呢？最大的好处就是多线程访问可以省去很多同步控制，因为它们是不可变的，一旦构造完成，就不会存在多线程竞争访问问题了。多线程最麻烦的处理就是控制好读写问题，如果大家都是读，那么就不存控制了，所以省去了很多同步操作。</p>
<p>更多关于Immutable 的介绍，可以参阅 <a href="https://en.wikipedia.org/wiki/Immutable_object" target="_blank" rel="external">wiki</a> 。</p>
<p>举个Java中的例子：String和StringBuilder，String是immutable的，每次对于String对象的修改都将产生一个新的String对象，而原来的对象保持不变，而StringBuilder是mutable，因为每次对于它的对象的修改都作用于该对象本身，并没有产生新的对象。</p>
<p>Immutable objects 比传统的mutable对象在多线程应用中更具有优势，它不仅能够保证对象的状态不被改变，而且还可以不使用锁机制就能被其他线程共享。</p>
<p>总结下Immutable对象的优缺点：</p>
<p><strong>优点</strong></p>
<ul>
<li>Immutable对象是线程安全的，可以不用被synchronize就在并发环境中共享</li>
<li>Immutable对象简化了程序开发，因为它无需使用额外的锁机制就可以在线程间共享</li>
<li>Immutable对象提高了程序的性能，因为它减少了synchroinzed的使用</li>
<li>Immutable对象是可以被重复使用的，你可以将它们缓存起来重复使用，就像字符串字面量和整型数字一样。你可以使用静态工厂方法来提供类似于valueOf（）这样的方法，它可以从缓存中返回一个已经存在的Immutable对象，而不是重新创建一个。</li>
</ul>
<p><strong>缺点</strong></p>
<p>Immutable也有一个缺点就是会制造大量垃圾，由于他们不能被重用而且对于它们的使用就是”用“然后”扔“，字符串就是一个典型的例子，它会创造很多的垃圾，给垃圾收集带来很大的麻烦。当然这只是个极端的例子，合理的使用immutable对象会创造很大的价值。</p>
<h1 id="高级使用"><a href="#高级使用" class="headerlink" title="高级使用"></a>高级使用</h1><h2 id="Nullable"><a href="#Nullable" class="headerlink" title="Nullable"></a>Nullable</h2><p>上面说过如果类中有对象类型的成员变量，那么是为非空的，但是在实际情况下，有的字段的是值就是为null，所以在申明时候可申明为Nullable就可以了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">import</span> android.support.annotation.Nullable;</div><div class="line"></div><div class="line"><span class="keyword">import</span> com.google.auto.value.AutoValue;</div><div class="line"><span class="keyword">import</span> com.google.gson.annotations.SerializedName;</div><div class="line"></div><div class="line"><span class="meta">@AutoValue</span></div><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">NullableUser</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@SerializedName</span>(<span class="string">"id"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">int</span> <span class="title">id</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="meta">@Nullable</span></div><div class="line">    <span class="meta">@SerializedName</span>(<span class="string">"name"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> String <span class="title">name</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> NullableUser <span class="title">newInstance</span><span class="params">(<span class="keyword">int</span> id, String name)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_NullableUser(id, name);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>生成代码：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">AutoValue_NullableUser</span> <span class="keyword">extends</span> <span class="title">NullableUser</span> </span>&#123;</div><div class="line"></div><div class="line">  <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> id;</div><div class="line">  <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line"></div><div class="line">  AutoValue_NullableUser(</div><div class="line">      <span class="keyword">int</span> id,</div><div class="line">      <span class="meta">@Nullable</span> String name) &#123;</div><div class="line">    <span class="keyword">this</span>.id = id;</div><div class="line">    <span class="keyword">this</span>.name = name;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>测试用例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span>(expected = NullPointerException.class)</div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testUserNullPointException</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</div><div class="line">    User.newInstance(<span class="number">100</span>, <span class="keyword">null</span>);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testUserNullable</span><span class="params">()</span> </span>&#123;</div><div class="line">    NullableUser user = NullableUser.newInstance(<span class="number">100</span>, <span class="string">"test"</span>);</div><div class="line"></div><div class="line">    System.out.println(<span class="string">"user = "</span> + user);</div><div class="line">    Assert.assertEquals(user.id(), <span class="number">100</span>);</div><div class="line">    Assert.assertEquals(user.name(), <span class="string">"test"</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="Gson序列化"><a href="#Gson序列化" class="headerlink" title="Gson序列化"></a>Gson序列化</h2><p>Gson 使用比较麻烦，在普通的Model中，只需要在字段上面添加 @SerializedName注解即可。但是使用AutoValue，稍微有点繁琐。</p>
<p>首先需要引入一个依赖包，这里是<a href="https://github.com/rharter/auto-value-gson" target="_blank" rel="external">Auto value gson Github</a>。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">provided <span class="string">'com.ryanharter.auto.value:auto-value-gson:0.4.4'</span></div><div class="line">apt <span class="string">'com.ryanharter.auto.value:auto-value-gson:0.4.4'</span></div></pre></td></tr></table></figure>
<p>其次申明的抽象类中，每个方法上面添加对应的注解，然后再添加一个<code>typeAdapter</code>方法，申明这个方法，Gson就会根据这个找到对应的adapter，如下所示。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@AutoValue</span></div><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@SerializedName</span>(<span class="string">"id"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="keyword">int</span> <span class="title">id</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="meta">@SerializedName</span>(<span class="string">"name"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> String <span class="title">name</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> User <span class="title">newInstance</span><span class="params">(<span class="keyword">int</span> id, String name)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_User(id, name);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> TypeAdapter&lt;User&gt; <span class="title">typeAdapter</span><span class="params">(Gson gson)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_User.GsonTypeAdapter(gson);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p><code>typeAdapter</code>方法模板如下，T就是你当前Model的名字，写完以后会出现错误，没事重新编译下就好了，这样就会重新生成了代码。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> TypeAdapter&lt;T&gt; <span class="title">typeAdapter</span><span class="params">(Gson gson)</span> </span>&#123;</div><div class="line">   <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_T.GsonTypeAdapter(gson);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>第三申明一个TypeAdapterFactory的一个实现类，这个类是abstract的，AutoValue也会自动生成其实现类。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@GsonTypeAdapterFactory</span></div><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">MyAdapterFactory</span> <span class="keyword">implements</span> <span class="title">TypeAdapterFactory</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> TypeAdapterFactory <span class="title">create</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AutoValueGson_MyAdapterFactory();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>最后是单元测试，在json字符串转Model的时候，会使用一个Gson对象，这个对象不是平常使用的对象，需要自定义配置一些东西，然后这里就用到了上面所申明的<code>MyAdapterFactory</code>。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testUserToJson</span><span class="params">()</span> </span>&#123;</div><div class="line">    User user = User.newInstance(<span class="number">100</span>, <span class="string">"test"</span>);</div><div class="line"></div><div class="line">    String json = <span class="keyword">new</span> Gson().toJson(user);</div><div class="line">    System.out.println(json);</div><div class="line"></div><div class="line">    Assert.assertEquals(<span class="string">"&#123;\"id\":100,\"name\":\"test\"&#125;"</span>, json);</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testUserParseFromJson</span><span class="params">()</span> </span>&#123;</div><div class="line">    String json = <span class="string">"&#123;\"id\":100,\"name\":\"test\"&#125;"</span>;</div><div class="line">    </div><div class="line">    <span class="comment">// 自定义的Gson对象，需要配置 MyAdapterFactory</span></div><div class="line">    Gson gson = <span class="keyword">new</span> GsonBuilder().registerTypeAdapterFactory(MyAdapterFactory.create()).create();</div><div class="line"></div><div class="line">    User user = gson.fromJson(json, User.class);</div><div class="line">    System.out.println(user);</div><div class="line">    Assert.assertNotNull(user);</div><div class="line">    Assert.assertEquals(user.name(), <span class="string">"test"</span>);</div><div class="line">    Assert.assertEquals(user.id(), <span class="number">100</span>);</div><div class="line"></div><div class="line">    NullableUser nullableUser = gson.fromJson(json, NullableUser.class);</div><div class="line">    System.out.println(nullableUser);</div><div class="line">    Assert.assertNotNull(nullableUser);</div><div class="line">    Assert.assertEquals(nullableUser.name(), <span class="string">"test"</span>);</div><div class="line">    Assert.assertEquals(nullableUser.id(), <span class="number">100</span>);</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="Serializable-amp-Parcelable"><a href="#Serializable-amp-Parcelable" class="headerlink" title="Serializable &amp; Parcelable"></a>Serializable &amp; Parcelable</h2><p>Serializable是Java自带的序列化方式，和AutoValue结合不影响原先使用，只需要在申明的Model中实现Serializable接口即可。</p>
<p>Parcelable是Android提供的序列化方式，如果需要和AutoValue结合使用，和Serializable基本差不多，实现相关接口，然后在Gradle文件引入相关apt依赖即可。</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div></pre></td><td class="code"><pre><div class="line">apt <span class="string">'com.ryanharter.auto.value:auto-value-parcel:0.2.5'</span></div><div class="line">// Optionally <span class="keyword">for</span> TypeAdapter support</div><div class="line">// compile <span class="string">'com.ryanharter.auto.value:auto-value-parcel-adapter:0.2.5'</span></div></pre></td></tr></table></figure>
<p><a href="https://github.com/rharter/auto-value-parcel" target="_blank" rel="external">auto-value-parcel Github地址</a></p>
<p>上面的<code>auto-value-parcel-adapter</code>是可选项，是auto-value-parcel提供自定义类型转化，相关使用可以参见Github地址。</p>
<p>检查下Autovalue自动给我们实现的代码，果然不出所料，全部自动生成了。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">AutoValue_User</span> <span class="keyword">extends</span> $<span class="title">AutoValue_User</span> </span>&#123;</div><div class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Parcelable.Creator&lt;AutoValue_User&gt; CREATOR = <span class="keyword">new</span> Parcelable.Creator&lt;AutoValue_User&gt;() &#123;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> AutoValue_User <span class="title">createFromParcel</span><span class="params">(Parcel in)</span> </span>&#123;</div><div class="line">      <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_User(</div><div class="line">          in.readInt(),</div><div class="line">          in.readString()</div><div class="line">      );</div><div class="line">    &#125;</div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="keyword">public</span> AutoValue_User[] newArray(<span class="keyword">int</span> size) &#123;</div><div class="line">      <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_User[size];</div><div class="line">    &#125;</div><div class="line">  &#125;;</div><div class="line"></div><div class="line">  AutoValue_User(<span class="keyword">int</span> id, String name) &#123;</div><div class="line">    <span class="keyword">super</span>(id, name);</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="meta">@Override</span></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">writeToParcel</span><span class="params">(Parcel dest, <span class="keyword">int</span> flags)</span> </span>&#123;</div><div class="line">    dest.writeInt(id());</div><div class="line">    dest.writeString(name());</div><div class="line">  &#125;</div><div class="line"></div><div class="line">  <span class="meta">@Override</span></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">describeContents</span><span class="params">()</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="Retrofit和Rxjava结合使用"><a href="#Retrofit和Rxjava结合使用" class="headerlink" title="Retrofit和Rxjava结合使用"></a>Retrofit和Rxjava结合使用</h2><p>Android 开发的时候，很多开发者使用Retrofit这个网络库，以及RxJava异步工具。下面举例如何结合使用AutoValue，Retrofit，Rxjava。</p>
<p>这里有个获取天气的接口，返回的结果是json，我们用这个来测试下Retrofit。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">// https://api.thinkpage.cn/v3/weather/now.json?key=x4qjfuniyu97mt9y&amp;location=beijing&amp;language=zh-Hans&amp;unit=c</div><div class="line">&#123;</div><div class="line">  &quot;results&quot;: [</div><div class="line">    &#123;</div><div class="line">      &quot;location&quot;: &#123;</div><div class="line">        &quot;id&quot;: &quot;WX4FBXXFKE4F&quot;,</div><div class="line">        &quot;name&quot;: &quot;北京&quot;,</div><div class="line">        &quot;country&quot;: &quot;CN&quot;,</div><div class="line">        &quot;path&quot;: &quot;北京,北京,中国&quot;,</div><div class="line">        &quot;timezone&quot;: &quot;Asia/Shanghai&quot;,</div><div class="line">        &quot;timezone_offset&quot;: &quot;+08:00&quot;</div><div class="line">      &#125;,</div><div class="line">      &quot;now&quot;: &#123;</div><div class="line">        &quot;text&quot;: &quot;霾&quot;,</div><div class="line">        &quot;code&quot;: &quot;31&quot;,</div><div class="line">        &quot;temperature&quot;: &quot;10&quot;</div><div class="line">      &#125;,</div><div class="line">      &quot;last_update&quot;: &quot;2016-12-02T14:45:00+08:00&quot;</div><div class="line">    &#125;</div><div class="line">  ]</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>申明Retrofit Api接口，一个普通的调用，一个是RxJava的方式。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">IWeatherApi</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@GET</span>(<span class="string">"/v3/weather/now.json?key=x4qjfuniyu97mt9y&amp;location=beijing&amp;language=zh-Hans&amp;unit=c"</span>)</div><div class="line">    <span class="function">Call&lt;Weather&gt; <span class="title">getWeather</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="meta">@GET</span>(<span class="string">"/v3/weather/now.json?key=x4qjfuniyu97mt9y&amp;location=beijing&amp;language=zh-Hans&amp;unit=c"</span>)</div><div class="line">    <span class="function">Observable&lt;Weather&gt; <span class="title">getWeatherWithRx</span><span class="params">()</span></span>;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Retrofit 接口创建</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">RetrofitUtil</span> </span>&#123;</div><div class="line">    <span class="keyword">public</span> <span class="keyword">static</span> &lt;T&gt; <span class="function">T <span class="title">createApi</span><span class="params">(@NonNull Class&lt;T&gt; tClass, Gson gson)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> Retrofit.Builder()</div><div class="line">                .baseUrl(<span class="string">"https://api.thinkpage.cn"</span>)</div><div class="line">                .client(<span class="keyword">new</span> OkHttpClient.Builder().build())</div><div class="line">                .addConverterFactory(GsonConverterFactory.create(gson))</div><div class="line">                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())</div><div class="line">                .build()</div><div class="line">                .create(tClass);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>Weather Model申明<br><figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">abstract</span> <span class="class"><span class="keyword">class</span> <span class="title">Weather</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@SerializedName</span>(<span class="string">"results"</span>)</div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">abstract</span> List&lt;ResultsItem&gt; <span class="title">results</span><span class="params">()</span></span>;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> TypeAdapter&lt;Weather&gt; <span class="title">typeAdapter</span><span class="params">(Gson gson)</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="keyword">new</span> AutoValue_Weather.GsonTypeAdapter(gson);</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure></p>
<p>测试用例，注意：Retrofit使用Gson和前面使用Gson使用方式一样，需要自己自定义，不然无法解决json解析问题。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testRetrofitWithAutoValue</span><span class="params">()</span> </span>&#123;</div><div class="line">    Gson gson = <span class="keyword">new</span> GsonBuilder().registerTypeAdapterFactory(MyAdapterFactory.create()).create();</div><div class="line">    IWeatherApi weatherApi = RetrofitUtil.createApi(IWeatherApi.class, gson);</div><div class="line">    <span class="keyword">try</span> &#123;</div><div class="line">        <span class="comment">// 同步调用</span></div><div class="line">        Weather weather = weatherApi.getWeather().execute().body();</div><div class="line">        Assert.assertNotNull(weather);</div><div class="line"></div><div class="line">        System.out.println(weather);</div><div class="line"></div><div class="line">        <span class="comment">// Rxjava 使用</span></div><div class="line">        weatherApi.getWeatherWithRx().subscribe(<span class="keyword">new</span> Action1&lt;Weather&gt;() &#123;</div><div class="line">            <span class="meta">@Override</span></div><div class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">call</span><span class="params">(Weather weather)</span> </span>&#123;</div><div class="line">                System.out.println(weather);</div><div class="line">            &#125;</div><div class="line">        &#125;);</div><div class="line">    &#125; <span class="keyword">catch</span> (IOException e) &#123;</div><div class="line">        e.printStackTrace();</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>运行结果，正常的返回天气信息。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">Weather&#123;results=[ResultsItem&#123;now=Now&#123;code=31, temperature=9, text=霾&#125;, lastUpdate=2016-12-02T14:15:00+08:00, location=Location&#123;country=CN, path=北京,北京,中国, timezone=Asia/Shanghai, timezoneOffset=+08:00, name=北京, id=WX4FBXXFKE4F&#125;&#125;]&#125;</div><div class="line">Weather&#123;results=[ResultsItem&#123;now=Now&#123;code=31, temperature=9, text=霾&#125;, lastUpdate=2016-12-02T14:15:00+08:00, location=Location&#123;country=CN, path=北京,北京,中国, timezone=Asia/Shanghai, timezoneOffset=+08:00, name=北京, id=WX4FBXXFKE4F&#125;&#125;]&#125;</div></pre></td></tr></table></figure>
<h1 id="相关插件"><a href="#相关插件" class="headerlink" title="相关插件"></a>相关插件</h1><h2 id="RoboPOJOGenerator"><a href="#RoboPOJOGenerator" class="headerlink" title="RoboPOJOGenerator"></a>RoboPOJOGenerator</h2><p>GsonFormat是一款Android Studio的插件，它可以把json字符串，转变成Model对象，很多人都喜欢用它。</p>
<p>但是如果使用了AutoValue，那么原先的插件就不能使用了，没有关系，本来打算自己高仿GsonFormat重新写了一个插件，以实现我们的需求，后面又发现有一款插件可以实现——RoboPOJOGenerator。</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/1.png" alt=""></p>
<p>RoboPOJOGenerator使用， <a href="https://github.com/robohorse/RoboPOJOGenerator" target="_blank" rel="external">RoboPOJOGenerator Github地址</a></p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/2.gif" alt=""></p>
<h2 id="AutoValue-plugin"><a href="#AutoValue-plugin" class="headerlink" title="AutoValue plugin"></a>AutoValue plugin</h2><p>上面我们发现有了json字符串，有时候还要写factory和buildder方法，那么问题来了，没有插件能帮我们实现这个步骤，然代码更加的优雅，开发更加高效？</p>
<p>答案是肯定的，Autovalue plugin就是干这个事的。</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/3.png" alt=""></p>
<p><a href="https://github.com/afcastano/AutoValuePlugin" target="_blank" rel="external">Auto value plugin Github</a></p>
<p>我们用刚刚上面的Weather做演示，相关演示：</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/4.gif" alt=""></p>
<h1 id="原理介绍"><a href="#原理介绍" class="headerlink" title="原理介绍"></a>原理介绍</h1><p>本文重点介绍的AutoValue只是 <a href="https://github.com/google/auto" target="_blank" rel="external">Google Auto</a> 中的一小部分，Auto中还有其他好玩的。</p>
<h2 id="AutoFactory"><a href="#AutoFactory" class="headerlink" title="AutoFactory"></a>AutoFactory</h2><p>AutoFactory和AutoValue类似，可以自动帮助代码生成工厂类，兼容Java 依赖注入标准（JSR-330）。</p>
<p>代码示例</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@AutoFactory</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">FactoryUser</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">int</span> id;</div><div class="line"></div><div class="line">    <span class="keyword">private</span> <span class="keyword">final</span> String name;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="title">FactoryUser</span><span class="params">(<span class="keyword">int</span> id, String name)</span> </span>&#123;</div><div class="line">        <span class="keyword">this</span>.id = id;</div><div class="line">        <span class="keyword">this</span>.name = name;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getId</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> id;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">getName</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> name;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">toString</span><span class="params">()</span> </span>&#123;</div><div class="line">        <span class="keyword">return</span> <span class="string">"FactoryUser&#123;"</span> +</div><div class="line">                <span class="string">"id="</span> + id +</div><div class="line">                <span class="string">", name='"</span> + name + <span class="string">'\''</span> +</div><div class="line">                <span class="string">'&#125;'</span>;</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>生成后的代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="keyword">final</span> <span class="class"><span class="keyword">class</span> <span class="title">FactoryUserFactory</span> </span>&#123;</div><div class="line">  <span class="meta">@Inject</span></div><div class="line">  <span class="function"><span class="keyword">public</span> <span class="title">FactoryUserFactory</span><span class="params">()</span> </span>&#123;</div><div class="line">  &#125;</div><div class="line">  <span class="function"><span class="keyword">public</span> FactoryUser <span class="title">create</span><span class="params">(<span class="keyword">int</span> id, String name)</span> </span>&#123;</div><div class="line">    <span class="keyword">return</span> <span class="keyword">new</span> FactoryUser(id, name);</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>测试代码</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@Test</span></div><div class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testFactoryUser</span><span class="params">()</span> </span>&#123;</div><div class="line">    FactoryUser user = <span class="keyword">new</span> FactoryUserFactory().create(<span class="number">100</span>, <span class="string">"test"</span>);</div><div class="line"></div><div class="line">    System.out.println(user);</div><div class="line">    Assert.assertNotNull(user);</div><div class="line">    Assert.assertEquals(<span class="number">100</span>, user.getId());</div><div class="line">    Assert.assertEquals(<span class="string">"test"</span>, user.getName());</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h2 id="AutoService"><a href="#AutoService" class="headerlink" title="AutoService"></a>AutoService</h2><p>AutoService比较简单，就是在使用Java APT的时候，使用AutoService注解，可以自动生成meta信息。</p>
<h2 id="AutoCommon"><a href="#AutoCommon" class="headerlink" title="AutoCommon"></a>AutoCommon</h2><p>这个是Google对Java Apt的一个扩展，一般的在自己写Apt的时候，都需要继承<code>AbstractProcessor</code>，但是google对它进行了扩展，<code>BasicAnnotationProcessor</code>，如果你想自己写个工具，那么就可以使用这个了。</p>
<p>给大家举个栗子，Dagger当初是Square公司受到Guice的启发，然后自己开发出一套依赖注入框架，当时Dagger使用的是Java反射，大家知道Java反射的效率其实并不高。</p>
<p>再后来都到了AutoValue的启发，在Dagger的分支上切个新分支，开发出Dagger2，然后这个Dagger2是由Google维护的，我们可以在Dagger2的Github上面找到证据。</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/5.png" alt=""></p>
<p>## </p>
<h1 id="Auto相关使用"><a href="#Auto相关使用" class="headerlink" title="Auto相关使用"></a>Auto相关使用</h1><h2 id="IntentBuilder"><a href="#IntentBuilder" class="headerlink" title="IntentBuilder"></a>IntentBuilder</h2><p>有时候几个Activity之间相互跳转的时候需要传递一些参数，这些参数可以是基本类型，也有可能是复杂的类型，如果是负责的类型，必须要实现Serializable 或 Parcelable接口，上面也有介绍。</p>
<p>下面推IntentBuilder，IntentBuilder也是利用代码生成的方法实现的。</p>
<p><a href="https://github.com/emilsjolander/IntentBuilder" target="_blank" rel="external">IntentBuilder Github</a></p>
<p>Activity传参</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@IntentBuilder</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">DetailActivity</span> <span class="keyword">extends</span> <span class="title">Activity</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Extra</span></div><div class="line">    String id;</div><div class="line"></div><div class="line">    <span class="meta">@Extra</span> <span class="meta">@Nullable</span></div><div class="line">    String title;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line">        DetailActivityIntentBuilder.inject(getIntent(), <span class="keyword">this</span>);</div><div class="line">        <span class="comment">// TODO use id and title</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 调用方式</span></div><div class="line">startActivity(</div><div class="line">    <span class="keyword">new</span> DetailActivityIntentBuilder(<span class="string">"12345"</span>)</div><div class="line">        .title(<span class="string">"MyTitle"</span>)</div><div class="line">        .build(context)</div><div class="line">)</div></pre></td></tr></table></figure>
<p>Service传参</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@IntentBuilder</span></div><div class="line"><span class="class"><span class="keyword">class</span> <span class="title">DownloadService</span> <span class="keyword">extends</span> <span class="title">IntentService</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Extra</span></div><div class="line">    String downloadUrl;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">onHandleIntent</span><span class="params">(Intent intent)</span> </span>&#123;</div><div class="line">        MyServiceIntentBuilder.inject(intent, <span class="keyword">this</span>);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">&#125;</div><div class="line"></div><div class="line">startService(<span class="keyword">new</span> DownloadServiceIntentBuilder(<span class="string">"http://google.com"</span>).build(context))</div></pre></td></tr></table></figure>
<h2 id="FragmentArgs"><a href="#FragmentArgs" class="headerlink" title="FragmentArgs"></a>FragmentArgs</h2><p>上面介绍了Activity、Service的传参，但Fragment的传参方式是不一样的，还有需要提醒一句一般通过setter方法给Fragment传参是不是正确的方式，必须通过setArgs的方式。</p>
<p><a href="https://github.com/sockeqwe/fragmentargs" target="_blank" rel="external">fragmentargs Github</a></p>
<p>相关代码示例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div></pre></td><td class="code"><pre><div class="line"><span class="meta">@FragmentWithArgs</span></div><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">MyFragment</span> <span class="keyword">extends</span> <span class="title">Fragment</span> </span>&#123;</div><div class="line">    <span class="meta">@Arg</span></div><div class="line">    <span class="keyword">int</span> id;</div><div class="line"></div><div class="line">    <span class="meta">@Override</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onCreate</span><span class="params">(Bundle savedInstanceState)</span> </span>&#123;</div><div class="line">        <span class="keyword">super</span>.onCreate(savedInstanceState);</div><div class="line">        FragmentArgs.inject(<span class="keyword">this</span>); </div><div class="line">        <span class="comment">// inject 之后，就可以使用 id 了</span></div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line"></div><div class="line">MyFragment fragment = MyFragmentBuilder.newMyFragment(<span class="number">101</span>);</div></pre></td></tr></table></figure>
<h1 id="其他相关"><a href="#其他相关" class="headerlink" title="其他相关"></a>其他相关</h1><h2 id="Kotlin-Data-Class"><a href="#Kotlin-Data-Class" class="headerlink" title="Kotlin Data Class"></a>Kotlin Data Class</h2><p>Kotlin 是一个基于 JVM 的新的编程语言，由 JetBrains 开发。有机会可以向大家介绍这种语言。</p>
<p>Kotlin 中提供一种类似于AutoValue中的功能，Data Class表示这个类似是一个数据类型。</p>
<p>比如下面是kotlin中对Model的写法，就是这么的简单、明了、优雅。</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">data</span> <span class="class"><span class="keyword">class</span> <span class="title">KotlinUser</span></span>(<span class="keyword">val</span> id: <span class="built_in">Int</span>,</div><div class="line">                      <span class="keyword">val</span> name: String)</div></pre></td></tr></table></figure>
<p>Kotlin与Java是可以相互调用的。下面是Java的测试用例。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">UserTest</span> </span>&#123;</div><div class="line"></div><div class="line">    <span class="meta">@Test</span></div><div class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">testUser</span><span class="params">()</span> </span>&#123;</div><div class="line">        KotlinUser user = <span class="keyword">new</span> KotlinUser(<span class="number">100</span>, <span class="string">"test"</span>);</div><div class="line"></div><div class="line">        System.out.println(user);</div><div class="line"></div><div class="line">        Assert.assertEquals(<span class="number">100</span>, user.getId());</div><div class="line">        Assert.assertEquals(<span class="string">"test"</span>, user.getName());</div><div class="line">    &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<p>我们可以反编译Kotlin生成的class字节码，看看这个中间到底发生了什么，很明显Kotlin做了很多的语法糖，这里编译器生成的代码和上面Autovalue生成的代码很像。</p>
<p><img src="https://raw.githubusercontent.com/LiushuiXiaoxia/AutoValueDemo/master/doc/6.png" alt=""></p>
<h2 id="Object-C"><a href="#Object-C" class="headerlink" title="Object-C"></a>Object-C</h2><p>Object-C中可以过直接申明@property方式，然后就可以自动实现setter和getter方法，如果要实现Immutable type方式，需要注明readonly。</p>
<p>hash、equals、description如果使用APPCode，代码是可以自动生成的。</p>
<figure class="highlight plain"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div><div class="line">53</div><div class="line">54</div><div class="line">55</div><div class="line">56</div><div class="line">57</div><div class="line">58</div><div class="line">59</div><div class="line">60</div><div class="line">61</div><div class="line">62</div><div class="line">63</div><div class="line">64</div><div class="line">65</div><div class="line">66</div><div class="line">67</div></pre></td><td class="code"><pre><div class="line">@interface OcUser : NSObject</div><div class="line"></div><div class="line">@property(readonly) int id;</div><div class="line"></div><div class="line">@property(retain, readonly) NSString *name;</div><div class="line"></div><div class="line">- (instancetype)initWithId:(int)id name:(NSString *)name;</div><div class="line"></div><div class="line">- (NSString *)description;</div><div class="line"></div><div class="line">- (BOOL)isEqual:(id)other;</div><div class="line"></div><div class="line">- (BOOL)isEqualToUser:(OcUser *)user;</div><div class="line"></div><div class="line">- (NSUInteger)hash;</div><div class="line"></div><div class="line">@end</div><div class="line"></div><div class="line">// ==========================</div><div class="line"></div><div class="line">#import &quot;OcUser.h&quot;</div><div class="line">@implementation OcUser &#123;</div><div class="line"></div><div class="line">&#125;</div><div class="line">- (instancetype)initWithId:(int)id name:(NSString *)name &#123;</div><div class="line">    self = [super init];</div><div class="line">    if (self) &#123;</div><div class="line">        _id   = id;</div><div class="line">        _name = name;</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    return self;</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (BOOL)isEqual:(id)other &#123;</div><div class="line">    if (other == self)</div><div class="line">        return YES;</div><div class="line">    if (!other || ![[other class] isEqual:[self class]])</div><div class="line">        return NO;</div><div class="line"></div><div class="line">    return [self isEqualToUser:other];</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (BOOL)isEqualToUser:(OcUser *)user &#123;</div><div class="line">    if (self == user)</div><div class="line">        return YES;</div><div class="line">    if (user == nil)</div><div class="line">        return NO;</div><div class="line">    if (self.id != user.id)</div><div class="line">        return NO;</div><div class="line">    return !(self.name != user.name &amp;&amp; ![self.name isEqualToString:user.name]);</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (NSUInteger)hash &#123;</div><div class="line">    NSUInteger hash = (NSUInteger) self.id;</div><div class="line">    hash = hash * 31u + [self.name hash];</div><div class="line">    return hash;</div><div class="line">&#125;</div><div class="line"></div><div class="line">- (NSString *)description &#123;</div><div class="line">    NSMutableString *description = [NSMutableString stringWithFormat:@&quot;&lt;%@: &quot;, NSStringFromClass([self class])];</div><div class="line">    [description appendFormat:@&quot;self.id=%i&quot;, self.id];</div><div class="line">    [description appendFormat:@&quot;, self.name=%@&quot;, self.name];</div><div class="line">    [description appendString:@&quot;&gt;&quot;];</div><div class="line">    return description;</div><div class="line">&#125;</div><div class="line">@end</div></pre></td></tr></table></figure>
<p>测试用例</p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#import <span class="meta-string">&lt;Foundation/Foundation.h&gt;</span></span></div><div class="line"><span class="meta">#import <span class="meta-string">"OcUser.h"</span></span></div><div class="line"></div><div class="line"><span class="keyword">int</span> main(<span class="keyword">int</span> argc, <span class="keyword">const</span> <span class="keyword">char</span> *argv[]) &#123;</div><div class="line">    <span class="keyword">@autoreleasepool</span> &#123;</div><div class="line">        OcUser *user = [[OcUser alloc] initWithId:<span class="number">100</span> name:<span class="string">@"test"</span>];</div><div class="line"></div><div class="line">        <span class="built_in">NSLog</span>(<span class="string">@"user = %@"</span>, user);</div><div class="line">    &#125;</div><div class="line"></div><div class="line">    <span class="keyword">return</span> <span class="number">0</span>;</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="comment">// 运行结果</span></div><div class="line"><span class="comment">// user = &lt;OcUser: self.id=100, self.name=test&gt;</span></div></pre></td></tr></table></figure>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>本文主要介绍了Autovalue的主要用法，以及AutoValu周边只是，可能说的比较多，比较杂，而且有的地方也不够深入，但是个人觉的这是一种思路，一种解决方案，后面如果自己需要造轮子的时候，我们是可以借鉴的。</p>
<p>本示例代码地址 <a href="https://github.com/LiushuiXiaoxia/AutoValueDemo" target="_blank" rel="external">AutoValueDemo</a></p>
<h1 id="参考连接"><a href="#参考连接" class="headerlink" title="参考连接"></a>参考连接</h1><p><a href="https://github.com/LiushuiXiaoxia/AutoValueDemo" target="_blank" rel="external">AutoValueDemo</a></p>
<p><a href="http://bigmercu.top/2016/09/08/%E5%AE%8C%E7%BE%8EModel%E4%B9%8BAutoValue%E4%BD%BF%E7%94%A8/" target="_blank" rel="external">完美Model之AutoValue使用</a></p>
<p><a href="http://blog.piasy.com/2016/05/06/Perfect-Android-Model-Layer/" target="_blank" rel="external">完美的安卓 model 层架构（上）</a></p>
<p><a href="http://blog.piasy.com/2016/05/12/Perfect-Android-Model-Layer-2/" target="_blank" rel="external">完美的安卓 model 层架构（下）</a></p>
<p><a href="https://github.com/google/auto/tree/master/value" target="_blank" rel="external">AutoValue Github</a></p>
<p><a href="https://my.oschina.net/jasonultimate/blog/166810" target="_blank" rel="external">Java Immutable 介绍</a></p>
<p><a href="https://github.com/rharter/auto-value-gson" target="_blank" rel="external">Auto value gson Github</a></p>
<p><a href="https://github.com/rharter/auto-value-parcel" target="_blank" rel="external">Auto value parcel Github</a></p>
<p><a href="https://github.com/robohorse/RoboPOJOGenerator" target="_blank" rel="external">RoboPOJOGenerator Github</a></p>
<p><a href="https://github.com/afcastano/AutoValuePlugin" target="_blank" rel="external">Auto value plugin Github</a></p>
<p><a href="https://github.com/emilsjolander/IntentBuilder" target="_blank" rel="external">IntentBuilder Github</a></p>
<p><a href="https://github.com/sockeqwe/fragmentargs" target="_blank" rel="external">Fragmentargs Github</a></p>

      
    </div>
    
    
    

    

    
      <div>
        <div style="padding: 10px 0; margin: 20px auto; width: 90%; text-align: center;">
  <div>坚持原创技术分享，您的支持将鼓励我继续创作！</div>
  <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
    <span>打赏</span>
  </button>
  <div id="QR" style="display: none;">

    
      <div id="wechat" style="display: inline-block">
        <img id="wechat_qr" src="/images/wechat_pay.jpeg" alt="流水不腐小夏 微信支付"/>
        <p>微信支付</p>
      </div>
    

    
      <div id="alipay" style="display: inline-block">
        <img id="alipay_qr" src="/images/alipay.jpeg" alt="流水不腐小夏 支付宝"/>
        <p>支付宝</p>
      </div>
    

    

  </div>
</div>

      </div>
    

    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/AutoValue/" rel="tag"># AutoValue</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2017/04/18/AutoValue2/" rel="next" title="安利AutoValue第二波">
                <i class="fa fa-chevron-left"></i> 安利AutoValue第二波
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2017/05/21/Android-Modular/" rel="prev" title="关于Android模块化我有一些话不知当讲不当讲">
                关于Android模块化我有一些话不知当讲不当讲 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
      <div id="disqus_thread">
        <noscript>
          Please enable JavaScript to view the
          <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a>
        </noscript>
      </div>
    </div>

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/avatar.jpeg"
                alt="流水不腐小夏" />
            
              <p class="site-author-name" itemprop="name">流水不腐小夏</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">31</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">55</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          
            <div class="feed-link motion-element">
              <a href="/atom.xml" rel="alternate">
                <i class="fa fa-rss"></i>
                RSS
              </a>
            </div>
          

          <div class="links-of-author motion-element">
            
              
                <span class="links-of-author-item">
                  <a href="https://github.com/LiushuiXiaoxia" target="_blank" title="GitHub">
                    
                      <i class="fa fa-fw fa-GitHub"></i>GitHub</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://www.jianshu.com/u/58771f980bac" target="_blank" title="简书">
                    
                      <i class="fa fa-fw fa-简书"></i>简书</a>
                </span>
              
                <span class="links-of-author-item">
                  <a href="http://blog.csdn.net/guijiaoba" target="_blank" title="CSDN">
                    
                      <i class="fa fa-fw fa-CSDN"></i>CSDN</a>
                </span>
              
            
          </div>

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#Android-Model正确使用姿势——AutoValue"><span class="nav-number">1.</span> <span class="nav-text">Android Model正确使用姿势——AutoValue</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#前言"><span class="nav-number">2.</span> <span class="nav-text">前言</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#简介"><span class="nav-number">3.</span> <span class="nav-text">简介</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#简单使用"><span class="nav-number">4.</span> <span class="nav-text">简单使用</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Immutable-Value-types"><span class="nav-number">5.</span> <span class="nav-text">Immutable/Value types</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#高级使用"><span class="nav-number">6.</span> <span class="nav-text">高级使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Nullable"><span class="nav-number">6.1.</span> <span class="nav-text">Nullable</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Gson序列化"><span class="nav-number">6.2.</span> <span class="nav-text">Gson序列化</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Serializable-amp-Parcelable"><span class="nav-number">6.3.</span> <span class="nav-text">Serializable & Parcelable</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Retrofit和Rxjava结合使用"><span class="nav-number">6.4.</span> <span class="nav-text">Retrofit和Rxjava结合使用</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#相关插件"><span class="nav-number">7.</span> <span class="nav-text">相关插件</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#RoboPOJOGenerator"><span class="nav-number">7.1.</span> <span class="nav-text">RoboPOJOGenerator</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#AutoValue-plugin"><span class="nav-number">7.2.</span> <span class="nav-text">AutoValue plugin</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#原理介绍"><span class="nav-number">8.</span> <span class="nav-text">原理介绍</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#AutoFactory"><span class="nav-number">8.1.</span> <span class="nav-text">AutoFactory</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#AutoService"><span class="nav-number">8.2.</span> <span class="nav-text">AutoService</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#AutoCommon"><span class="nav-number">8.3.</span> <span class="nav-text">AutoCommon</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#Auto相关使用"><span class="nav-number">9.</span> <span class="nav-text">Auto相关使用</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#IntentBuilder"><span class="nav-number">9.1.</span> <span class="nav-text">IntentBuilder</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#FragmentArgs"><span class="nav-number">9.2.</span> <span class="nav-text">FragmentArgs</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#其他相关"><span class="nav-number">10.</span> <span class="nav-text">其他相关</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#Kotlin-Data-Class"><span class="nav-number">10.1.</span> <span class="nav-text">Kotlin Data Class</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Object-C"><span class="nav-number">10.2.</span> <span class="nav-text">Object-C</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">11.</span> <span class="nav-text">总结</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#参考连接"><span class="nav-number">12.</span> <span class="nav-text">参考连接</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2015 &mdash; <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">流水不腐小夏</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Gemini</a> v5.1.3</div>




        
<div class="busuanzi-count">
  <script async src="https://dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  

  
</div>








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.3"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.3"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.3"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.3"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.3"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.3"></script>



  


  

    
      <script id="dsq-count-scr" src="https://mycommons-cn.disqus.com/count.js" async></script>
    

    
      <script type="text/javascript">
        var disqus_config = function () {
          this.page.url = 'http://mycommons.cn/2017/05/11/AutoValue/';
          this.page.identifier = '2017/05/11/AutoValue/';
          this.page.title = 'Android Model正确使用姿势——AutoValue';
        };
        var d = document, s = d.createElement('script');
        s.src = 'https://mycommons-cn.disqus.com/embed.js';
        s.setAttribute('data-timestamp', '' + +new Date());
        (d.head || d.body).appendChild(s);
      </script>
    

  




	





  














  




  
  
  
  <link rel="stylesheet" href="/lib/algolia-instant-search/instantsearch.min.css">

  
  
  <script src="/lib/algolia-instant-search/instantsearch.min.js"></script>
  

  <script src="/js/src/algolia-search.js?v=5.1.3"></script>



  

  

  
  

  
  
    <script type="text/x-mathjax-config">
      MathJax.Hub.Config({
        tex2jax: {
          inlineMath: [ ['$','$'], ["\\(","\\)"]  ],
          processEscapes: true,
          skipTags: ['script', 'noscript', 'style', 'textarea', 'pre', 'code']
        }
      });
    </script>

    <script type="text/x-mathjax-config">
      MathJax.Hub.Queue(function() {
        var all = MathJax.Hub.getAllJax(), i;
        for (i=0; i < all.length; i += 1) {
          all[i].SourceElement().parentNode.className += ' has-jax';
        }
      });
    </script>
    <script type="text/javascript" src="//cdn.bootcss.com/mathjax/2.7.1/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
  


  

  

</body>
</html>
